package com.hrsoft.edi.stub;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.apache.commons.lang3.StringUtils;

import com.hrsoft.edi.stub.consant.AppInfo;
import com.hrsoft.edi.stub.request.DeliveryorderCreateRequest;
import com.qimen.api.DefaultQimenClient;
import com.qimen.api.QimenClient;
import com.qimen.api.response.DeliveryorderCreateResponse;
import com.taobao.api.ApiException;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;

public class DeliveryOrderTest
{
    public static void create()
        throws ApiException
    {
        QimenClient client = new DefaultQimenClient(AppInfo.URL, AppInfo.APP_KEY, AppInfo.SECRET);
        DeliveryorderCreateRequest createRequest = new DeliveryorderCreateRequest();
        // 如果使用混合订单接口 打开apiname赋值的代码
//        createRequest.setApiName("mixorder.create");
        createRequest.setCustomerId(AppInfo.CUSTOMER_ID);
        createRequest.setVersion("2.0");
        DeliveryorderCreateRequest.DeliveryOrder deliveryOrder = new DeliveryorderCreateRequest.DeliveryOrder();
        deliveryOrder.setWarehouseCode(AppInfo.WAREHOUSE_CODE);
        deliveryOrder.setLogisticsCode("SF");
        deliveryOrder.setDeliveryOrderCode("DZL2C" + RandomUtil.randomNumbers(8));
        // deliveryOrder.setExpressCode(deliveryOrder.getDeliveryOrderCode());
        deliveryOrder.setOrderType("JYCK");
        deliveryOrder.setSourcePlatformCode("TB");
        deliveryOrder.setCreateTime(DateUtil.formatDateTime(new Date()));
        deliveryOrder.setPlaceOrderTime(DateUtil.formatDateTime(new Date()));
        deliveryOrder.setOperateTime(DateUtil.formatDateTime(new Date()));
        deliveryOrder.setShopNick("小风扇淘宝旗舰店");
        DeliveryorderCreateRequest.SenderInfo senderInfo = new DeliveryorderCreateRequest.SenderInfo();
        senderInfo.setName("马云");
        senderInfo.setMobile("13887898786");
        senderInfo.setProvince("福建省");
        senderInfo.setCity("泉州市");
        senderInfo.setTown("鲤城区");
        senderInfo.setDetailAddress("清濛开发区崇益街19号利讯3楼仓库");
        
        DeliveryorderCreateRequest.ReceiverInfo receiverInfo = new DeliveryorderCreateRequest.ReceiverInfo();
        receiverInfo.setName("马小云");
        receiverInfo.setMobile("13887898786");
        receiverInfo.setProvince("浙江省");
        receiverInfo.setCity("杭州市");
        receiverInfo.setArea("西湖区");
        receiverInfo.setDetailAddress("西湖大街3栋504");
        receiverInfo.setCompany("苹果专卖店");
        deliveryOrder.setReceiverInfo(receiverInfo);
        deliveryOrder.setSenderInfo(senderInfo);
        createRequest.setDeliveryOrder(deliveryOrder);
        List<DeliveryorderCreateRequest.OrderLine> orderLines = new ArrayList<>();
        
        String key = StringUtils.leftPad("" + RandomUtil.randomInt(1, 1000), 4, "0");
        DeliveryorderCreateRequest.OrderLine orderLine = new DeliveryorderCreateRequest.OrderLine();
        orderLine.setOrderLineNo(RandomUtil.randomNumbers(15));
        orderLine.setItemCode("0001");
        orderLine.setPlanQty("1");
        orderLine.setActualPrice("1");
        orderLine.setOwnerCode(AppInfo.OWNER_CODE);
        orderLine.setInventoryType("ZP");
        orderLines.add(orderLine);
        // DeliveryorderCreateRequest.OrderLine orderLine2 = new DeliveryorderCreateRequest.OrderLine();
        // orderLine2.setOrderLineNo(RandomUtil.randomNumbers(15));
        // orderLine2.setItemCode("001");
        // orderLine2.setPlanQty("1");
        // orderLine2.setActualPrice("1");
        // orderLine2.setOwnerCode(OWNER_CODE);
        // orderLine2.setInventoryType("ZP");
        // orderLines.add(orderLine2);
        createRequest.setOrderLines(orderLines);
        long time1 = System.currentTimeMillis();
        DeliveryorderCreateResponse response = client.execute(createRequest);
        long time2 = System.currentTimeMillis();
        System.out.println(response.getCode() + response.getFlag() + response.getMessage()
            + response.getDeliveryOrderId() + " cost:" + (time2 - time1) + " " + deliveryOrder.getDeliveryOrderCode());
    }
    
    public static void main(String[] args)
        throws ApiException
    {
        create();
    }
}